package com.baymax.study.tuLing.nio.test;

import org.junit.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * Demo01
 *
 * @author Baymax Gu
 * @since 2024-09-14
 */
public class Demo01 {
    @Test
    public void test1() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("Hello World".getBytes());
        buffer.flip();
        System.out.println(new String(buffer.array(), 0, buffer.remaining()));
        System.out.println(new String(buffer.array(), 0, buffer.limit()));
    }

    @Test
    public void test2() throws IOException {
        ServerSocketChannel socketChannel = ServerSocketChannel.open();
        socketChannel.configureBlocking(false);
        socketChannel.bind(new InetSocketAddress(8888));
        Selector selector = Selector.open();
        socketChannel.register(selector, SelectionKey.OP_ACCEPT);
    }
}
